package com.application.DataProvider;

import com.application.DTO.ClassDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ClassDAO{

	Context _mcontext = null;

	public ClassDAO(Context context){
		this._mcontext = context;
	}

	public boolean isExist(int id)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from Class where ID=" +  id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}
	
	public ClassDTO getClass(int ID)
	{
		ClassDTO classDto = new ClassDTO();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from Class where ID = " + ID;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				classDto.set_ID(Integer.valueOf(cur.getString(0)));
				classDto.set_Name(cur.getString(1));
				classDto.set_Note(cur.getString(2));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return classDto;
	}

	//Insert du lieu vao bang Class
	public int insertClass(ClassDTO classDto) {
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("ID", classDto.get_ID());
			values.put("Name", classDto.get_Name());
			values.put("Note", classDto.get_Note());

			result = provider.execInsertCommand(db, "Class", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	//Cap nhat bang Class
	public int updateClass(ClassDTO classDto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("Name", classDto.get_Name());
			values.put("Note", classDto.get_Note());

			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(classDto.get_ID())};
			result= provider.execUpdateCommand(db, "Class", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}
		finally {
			db.close();
		}
		return result;
	}

	//Delete doi tuong Class
	public int deleteClass(int id)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(id)};	
			result=provider.execDeleteCommand(db, "Class", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally {
			db.close();
		}
		return result;
	}

	
}
